#include "FilledQuadrilateral.h"

FilledQuadrilateral::FilledQuadrilateral()
{
    m_texture = 0;
}

FilledQuadrilateral::FilledQuadrilateral(QList<QPoint*> vertexs)
{
    m_texture = 0;
    m_vertexs = vertexs;
}

FilledQuadrilateral::FilledQuadrilateral(QList<QPoint*> vertexs,QList<QColor*> colors)
{
    m_texture = 0;
    m_vertexs = vertexs;
    m_colors = colors;
}

void FilledQuadrilateral::draw(QPainter* painter)
{
    QPoint* p1 = m_vertexs.at(0);
    QPoint* p2 = m_vertexs.at(1);
    QPoint* p3 = m_vertexs.at(2);
    QPoint* p4 = m_vertexs.at(3);

    if(texture() && !texture()->isSetten())
    {
        int xMin = min(p1->x(),p2->x());
        xMin = min(xMin,p3->x());
        xMin = min(xMin,p4->x());

        int xMax = max(p1->x(),p2->x());
        xMax = max(xMax,p3->x());
        xMax = max(xMax,p4->x());

        int yMin = min(p1->y(),p2->y());
        yMin = min(yMin,p3->y());
        yMin = min(yMin,p4->y());

        int yMax = max(p1->y(),p2->y());
        yMax = max(yMax,p3->y());
        yMax = max(yMax,p4->y());

        float largura = xMax - xMin;
        float altura = yMax - yMin;

        texture()->setObjectValues(xMin,yMax,largura,altura);
    }

    Triangle* t1 = new Triangle(p1,p2,p3);
    if(texture()) t1->setTexture(texture());
    t1->setColors(colors().mid(0,3));
    t1->setFilled(true);
    t1->setWired(false);
    t1->draw(painter);

    Triangle* t2 = new Triangle(p3,p4,p1);
    if(texture()) t2->setTexture(texture());
    t2->setColors(colors().mid(2,2) << colors().mid(0,1));
    t2->setFilled(true);
    t2->setWired(false);
    t2->draw(painter);
}
